package com.ruoyi.web.controller.system;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.system.domain.TmcEmergencyAttach;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.RecordEnclosu;
import com.ruoyi.system.service.IRecordEnclosuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 公告上报附件
Controller
 * 
 * @author ruoyi
 * @date 2021-08-20
 */
@RestController
@RequestMapping("/system/recordEnclosu/enclosu")
public class RecordEnclosuController extends BaseController
{
    @Autowired
    private IRecordEnclosuService recordEnclosuService;

    /**
     * 查询公告上报附件
列表
     */
//    @PreAuthorize("@ss.hasPermi('system:enclosu:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecordEnclosu recordEnclosu)
    {
        startPage();
        List<RecordEnclosu> list = recordEnclosuService.selectRecordEnclosuList(recordEnclosu);
        return getDataTable(list);
    }

    /**
     * 导出公告上报附件
列表
     */
//    @PreAuthorize("@ss.hasPermi('system:enclosu:export')")
    @Log(title = "公告上报附件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RecordEnclosu recordEnclosu)
    {
        List<RecordEnclosu> list = recordEnclosuService.selectRecordEnclosuList(recordEnclosu);
        ExcelUtil<RecordEnclosu> util = new ExcelUtil<RecordEnclosu>(RecordEnclosu.class);
        return util.exportExcel(list, "enclosu");
    }

    /**
     * 获取公告上报附件
详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:enclosu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(recordEnclosuService.selectRecordEnclosuById(id));
    }

    /**
     * 新增公告上报附件

     */
//    @PreAuthorize("@ss.hasPermi('system:enclosu:add')")
    @Log(title = "公告上报附件", businessType = BusinessType.INSERT)
    @PostMapping
    public Map add(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "erId") Long erId) {
        try {
            String profile = RuoYiConfig.getProfile();
            byte[] bytes = file.getBytes();
            //文件夹
            Path path2 = Paths.get(profile+"//erId//" + erId);
            //具体文件
            Path path = Paths.get(profile+"//erId//" + erId + "//" + file.getOriginalFilename());
            File file1 = path.toFile();
            //创建文件夹
            if (!file1.exists()) {
                path2.toFile().mkdirs();
            }
            if (file1.isFile()) {
                return AjaxResult.error("文件上传失败,文件存在");
            } else {
                Files.write(path, bytes);//创建文件
                if (true) {
                    RecordEnclosu recordEnclosu= new RecordEnclosu();
                    recordEnclosu.setErUrl("profile/erId/" + erId + "/" + file.getOriginalFilename());
                    recordEnclosu.setErId(erId);
                    recordEnclosuService.insertRecordEnclosu(recordEnclosu);
                    HashMap map1=new HashMap();
                    map1.put("ID", recordEnclosu.getId());
                    map1.put("Status", 0);
                    map1.put("ErrorMessage", "上传应急事件成功");
                    return map1;
                } else {
                    if (file1.isFile()) {
                        file1.delete();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 修改公告上报附件

     */
//    @PreAuthorize("@ss.hasPermi('system:enclosu:edit')")
    @Log(title = "公告上报附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecordEnclosu recordEnclosu)
    {
        return toAjax(recordEnclosuService.updateRecordEnclosu(recordEnclosu));
    }

    /**
     * 删除公告上报附件

     */
//    @PreAuthorize("@ss.hasPermi('system:enclosu:remove')")
    @Log(title = "公告上报附件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(recordEnclosuService.deleteRecordEnclosuByIds(ids));
    }
}
